import networkx as nx

from flask import Flask, jsonify, render_template
from .data_extractor import extract_nodes, extract_edges
from .knowledge_graph import NetworkXKG

node_label_formatter = None
format_edge_label = None
G: NetworkXKG = None

app = Flask(__name__, static_folder="static", static_url_path="", template_folder="static")


def init(node_label_formatter_, edge_label_formatter_, G_):
    global node_label_formatter, format_edge_label, G
    node_label_formatter = node_label_formatter_
    format_edge_label = edge_label_formatter_
    G = G_


@app.route("/graph", methods=["GET"])
def get_graph():
    nodes = extract_nodes(G)
    edges = extract_edges(G)
    return jsonify({"nodes": nodes, "edges": edges})


@app.route("/", methods=["GET"])
def index():
    return render_template("index.html", tree_data=[{"name": "root", "children": []}])


if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)
